﻿/** 
 * @fileOverview this file includes access logics to the twitter with OAuth.
 * 
 * @author Takahiro
 * @version 1.0.0
 */
 
 
/**
 * Constructer.
 * 
 * @class this class is responsible for access to thw twitter with OAuth.
 * 
 * @param {String} cunsumer_token consumer token.
 * @param {String} consumer_token_secret consumer token secret.
 */

var OAuthTwitterCore = function( _consumer_token, _consumer_token_secret ) {

  this.ACCESS_TOKEN_KEY        = 'oauth_twitter_access_token' ;
  this.ACCESS_TOKEN_SECRET_KEY = 'oauth_twitter_access_token_secret' ;

  this.consumer_token        = _consumer_token ;
  this.consumer_token_secret = _consumer_token_secret ;
  this.access_token          = localStorage.getItem( this.ACCESS_TOKEN_KEY ) ;
  this.access_token_secret   = localStorage.getItem( this.ACCESS_TOKEN_SECRET_KEY ) ;
  this.request_token         = '' ;
  this.request_token_secret  = '' ;

} ;

OAuthTwitterCore.prototype.sendAuthorizeUrl = function( _callback ) {
  var url = 'http://twitter.com/oauth/authorize?oauth_token=' + this.request_token ;
  _callback( { 'action' : 'gotRequestToken', 'url' : url } ) ;
} ;

OAuthTwitterCore.prototype.isLogined = function( ) {

  if( ! this.access_token || ! this.access_token_secret )
    return false ;

  return true ;

} ;

OAuthTwitterCore.prototype.login = function( ) {

  if( this.isLogined( ) ) {
    return true ;
  }

} ;

OAuthTwitterCore.prototype.getRequestToken = function( _callback ) {

  var self = this ;

  if( this.request_token && this.request_token_secret ) {
    this.sendAuthorizeUrl( _callback ) ;
    return ;
  }

  var accessor = {
    consumerSecret: this.consumer_token_secret,
    tokenSecret: ''
  } ;

  var message = {
    method: "GET", 
    action: 'http://twitter.com/oauth/request_token', 
    parameters: { 
      oauth_signature_method: "HMAC-SHA1", 
      oauth_consumer_key: this.consumer_token
    }
  } ;

  OAuth.setTimestampAndNonce( message ) ;
  OAuth.SignatureMethod.sign( message, accessor ) ;
  var target = OAuth.addToURL( message.action, message.parameters ) ;
  var options = {
    type: message.method,
    url: target,
    success: function( d, dt ) { 
      d.search( /oauth_token=([\w-]+)\&/ ) ; 
      self.request_token = RegExp.$1 ; 
      d.search( /oauth_token_secret=([\w-]+)\&/ ) ;
      self.request_token_secret = RegExp.$1 ;
      self.sendAuthorizeUrl( _callback ) ;
    },
    error: function( a ) {
      window.alert( a.responseText ) ;
    },
    timeout: 1000 * 50
  };

  $.ajax( options ) ;

} ;

OAuthTwitterCore.prototype.getAccessToken= function( _verifier, _callback ) {

  var self = this ;

  var accessor = {
    consumerSecret: OAUTH_CONSUMER_SECRET,
    tokenSecret: this.token_secret
  } ;

  var message = {
    method: "GET", 
    action: 'http://twitter.com/oauth/access_token', 
    parameters: { 
      oauth_signature_method: "HMAC-SHA1", 
      oauth_consumer_key: this.consumer_key,
      oauth_token: this.request_token,
      oauth_verifier: _verifier
    }
  } ;

  OAuth.setTimestampAndNonce( message ) ;
  OAuth.SignatureMethod.sign( message, accessor ) ;
  var target = OAuth.addToURL( message.action, message.parameters ) ;
  var options = {
    type: message.method,
    url: target,
    success: function( d, dt ) { 
      d.search( /oauth_token=([\w-]+)\&/ ) ; 
      self.access_token = RegExp.$1 ; 
      d.search( /oauth_token_secret=([\w-]+)\&/ ) ; 
      self.access_token_secret = RegExp.$1 ; 
      localStorage.setItem( self.ACCESS_TOKEN_KEY,        self.access_token ) ;
      localStorage.setItem( self.ACCESS_TOKEN_SECRET_KEY, self.access_token_secret ) ;
    },
    error: function( a ) {
      window.alert( a.responseText ) ;
    },
    timeout: 1000 * 50
  } ;

  $.ajax( options ) ;

} ;

OAuthTwitterCore.prototype.getHomeTimeline = function( _callback ) {

  var accessor = {
    consumerSecret: this.consumer_token_secret,
    tokenSecret: this.access_token_secret
  } ;

  var message = {
    method: "GET", 
    action: 'http://api.twitter.com/1/statuses/home_timeline.json', 
    parameters: { 
      oauth_signature_method: "HMAC-SHA1", 
      oauth_consumer_key: this.cunsumer_token, 
      oauth_token: this.access_token
    }
  } ;

  OAuth.setTimestampAndNonce( message ) ;
  OAuth.SignatureMethod.sign( message, accessor ) ;
  var target = OAuth.addToURL( message.action, message.parameters ) ;
  var options = {
    type: message.method,
    url: target,
    dataType: 'json',
    success: function( d, dt ) { 
      _callback( { 'action' : 'gotStatuses', 'statuses' : d } ) ;
    },
    error: function( a ) {
      window.alert( a.responseText ) ;
    },
    timeout: 1000 * 50
  };

  $.ajax( options ) ;

} ;


OAuthTwitterCore.prototype.updateStatus = function( _text, _callback ) {

  var accessor = {
    consumerSecret: this.consumer_token_secret,
    tokenSecret: this.access_token_secret
  } ;

  var message = {
    method: "POST", 
    action: 'http://api.twitter.com/1/statuses/update.json' , 
    parameters: { 
      oauth_signature_method: "HMAC-SHA1", 
      oauth_consumer_key: this.cunsumer_token, 
      oauth_token: this.access_token,
      status: _text
    }
  } ;

  OAuth.setTimestampAndNonce( message ) ;
  OAuth.SignatureMethod.sign( message, accessor ) ;
  var target = OAuth.addToURL( message.action, message.parameters ) ;
  var options = {
    type: message.method,
    url: target,
    dataType: 'json',
    success: function( d, dt ) {
      _callback( { 'action' : 'updatedStatus', 'text' : text } ) ;
    },
    error: function( a ) {
      window.alert( a.responseText ) ;
    },
    timeout: 1000 * 50
  };

  $.ajax( options ) ;

} ;
